package net.huashitong.Project.service;

import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.*;

import static net.huashitong.utils.UtilsTools.getCurrenYear;
import static net.huashitong.utils.UtilsTools.getCurrentMonth;

@Service
public class MyProjectsService {

    @Autowired
    private NewProjectsService newProjectsService;

    public Map<String, Object> queryStatisticsSummary(BnpUser bnpUser) {
        Map<String, Object> result = new TreeMap<>();
        NewProjectsFilter npf = new NewProjectsFilter();
        if (ObjectUtils.isNotBlank(bnpUser)) {
            if ("0".equals(bnpUser.getBranchType())) {
                if ("经办人".equals(bnpUser.getRole())) {
                    npf.setContactPhone(bnpUser.getPhone());
                } else if ("责任人".equals(bnpUser.getRole())) {
                    npf.setOwnerUnitPersonPhone(bnpUser.getPhone());
                }
            } else if ("6".equals(bnpUser.getBranchType())) {
                //领导
                if ("领导".equals(bnpUser.getRole())) {
                    npf.setResPhone(bnpUser.getPhone());
                }
            }
        }
        //行业分类
        /**
         * industryType
         * 1001 旅游特色小镇项目
         * 1002 工业项目
         * 1003 交通
         * 1004 城镇基础设施
         * 1005 安置房项目
         * 1006 社会事业项目
         * 1007 商住项目
         * 1008 农林水利环保生态项目
         */
        Map<String, Object> industryTypeMap = new HashMap<>();
        Map<String, Object> industryTypeMapMoney = new HashMap<>();
        //项目分类
        /**
         * projectType
         * 2001 计划新开工项目
         * 2002 结转项目
         */
        Map<String, Object> projectTypeMap = new HashMap<>();
        Map<String, Object> projectTypeMapMoney = new HashMap<>();
        //项目状态
        /**
         * status
         * 1 审批部门未确认
         * 2 未审核
         * 3 节点进行中
         * 6 已暂停
         * 4 已完成
         */
        Map<String, Object> statusMap = new HashMap<>();


        Map<String, Object> startMap = new HashMap<>();
        Map<String, Object> noStartMap = new HashMap<>();

        List<NewProjects> resList = newProjectsService.queryByfilter(npf);
        double moneyCount = 0.0;
        for (NewProjects projects : resList) {
            //industryType行业分类==================================================
            if (!industryTypeMap.containsKey(projects.getIndustryType())) {
                industryTypeMap.put(projects.getIndustryType(), 1);
            } else {
                int count = (int) industryTypeMap.get(projects.getIndustryType());
                industryTypeMap.put(projects.getIndustryType(), count + 1);
            }
            if (!industryTypeMapMoney.containsKey(projects.getIndustryType())) {
                industryTypeMapMoney.put(projects.getIndustryType(), Double.parseDouble(projects.getMoney()));
            } else {
                double count = (double) industryTypeMapMoney.get(projects.getIndustryType());
                industryTypeMapMoney.put(projects.getIndustryType(), count + Double.parseDouble(projects.getMoney()));
            }
            //开工项目Map
            if (!startMap.containsKey(projects.getIndustryType())) {
                if ("4".equals(projects.getStatus())) {
                    startMap.put(projects.getIndustryType(), 1);
                }
            } else {
                if ("4".equals(projects.getStatus())) {
                    int count = (int) startMap.get(projects.getIndustryType());
                    startMap.put(projects.getIndustryType(), count + 1);
                }
            }
            //未开工项目Map
            if (!noStartMap.containsKey(projects.getIndustryType())) {
                if (!"4".equals(projects.getStatus())) {
                    noStartMap.put(projects.getIndustryType(), 1);
                }
            } else {
                if (!"4".equals(projects.getStatus())) {
                    int count = (int) noStartMap.get(projects.getIndustryType());
                    noStartMap.put(projects.getIndustryType(), count + 1);
                }
            }


            //projectType项目分类==================================================
            if (!projectTypeMap.containsKey(projects.getProjectType())) {
                projectTypeMap.put(projects.getProjectType(), 1);
            } else {
                int count = (int) projectTypeMap.get(projects.getProjectType());
                projectTypeMap.put(projects.getProjectType(), count + 1);
            }
            if (!projectTypeMapMoney.containsKey(projects.getProjectType())) {
                projectTypeMapMoney.put(projects.getProjectType(), Double.parseDouble(projects.getMoney()));
            } else {
                double count = (double) projectTypeMapMoney.get(projects.getProjectType());
                projectTypeMapMoney.put(projects.getProjectType(), count + Double.parseDouble(projects.getMoney()));
            }

            //开工项目Map
            if (!startMap.containsKey(projects.getProjectType())) {
                if ("4".equals(projects.getStatus())) {
                    startMap.put(projects.getProjectType(), 1);
                }
            } else {
                if ("4".equals(projects.getStatus())) {
                    int count = (int) startMap.get(projects.getProjectType());
                    startMap.put(projects.getProjectType(), count + 1);
                }
            }
            //未开工项目Map
            if (!noStartMap.containsKey(projects.getProjectType())) {
                if (!"4".equals(projects.getStatus())) {
                    noStartMap.put(projects.getProjectType(), 1);
                }
            } else {
                if (!"4".equals(projects.getStatus())) {
                    int count = (int) noStartMap.get(projects.getProjectType());
                    noStartMap.put(projects.getProjectType(), count + 1);
                }
            }

            //status项目状态==================================================
            if (!statusMap.containsKey(projects.getStatus())) {
                statusMap.put(projects.getStatus(), 1);
            } else {
                int count = (int) statusMap.get(projects.getStatus());
                statusMap.put(projects.getStatus(), count + 1);
            }
            moneyCount = moneyCount + Double.parseDouble(projects.getMoney());
        }

        List<Map<String, Object>> industryTypeMapList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : industryTypeMap.entrySet()) {
            Map<String, Object> industryType = new HashMap<>();
            String name;
            String code;
            switch (entry.getKey()) {
                case "1001":
                    code = "1001";
                    name = "旅游特色小镇项目";
                    break;
                case "1002":
                    code = "1002";
                    name = "工业项目";
                    break;
                case "1003":
                    code = "1003";
                    name = "交通";
                    break;
                case "1004":
                    code = "1004";
                    name = "城镇基础设施";
                    break;
                case "1005":
                    code = "1005";
                    name = "安置房项目";
                    break;
                case "1006":
                    code = "1006";
                    name = "社会事业项目";
                    break;
                case "1007":
                    code = "1007";
                    name = "商住项目";
                    break;
                case "1008":
                    code = "1008";
                    name = "农林水利环保生态项目";
                    break;
                default:
                    code = "";
                    name = "";
            }
            String count = entry.getValue() + "";
            double money = (double) industryTypeMapMoney.get(entry.getKey());

            Object start = startMap.get(entry.getKey());
            if (ObjectUtils.isBlank(start)) {
                industryType.put("startProjects", 0);
            } else {
                industryType.put("startProjects", start);
            }
            Object noStart = noStartMap.get(entry.getKey());
            if (ObjectUtils.isBlank(noStart)) {
                industryType.put("noStartProjects", 0);
            } else {
                industryType.put("noStartProjects", noStart);
            }
            industryType.put("code", code);
            industryType.put("name", name);
            industryType.put("count", count);
            industryType.put("money", money);
            industryTypeMapList.add(industryType);
        }


        List<Map<String, Object>> projectTypeMapList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : projectTypeMap.entrySet()) {
            Map<String, Object> projectType = new HashMap<>();
            System.out.println("key= " + entry.getKey() + " and value= "
                    + entry.getValue());
            String name;
            String code;
            switch (entry.getKey()) {
                case "2001":
                    code = "2001";
                    name = "计划新开工项目";
                    break;
                case "2002":
                    code = "2002";
                    name = "结转项目";
                    break;
                default:
                    code = "";
                    name = "";
            }
            String count = entry.getValue() + "";
            double money = (double) projectTypeMapMoney.get(entry.getKey());
            projectType.put("code", code);
            projectType.put("name", name);
            projectType.put("count", count);
            projectType.put("money", money);

            Object start = startMap.get(entry.getKey());
            if (ObjectUtils.isBlank(start)) {
                projectType.put("startProjects", 0);
            } else {
                projectType.put("startProjects", start);
            }
            Object noStart = noStartMap.get(entry.getKey());
            if (ObjectUtils.isBlank(noStart)) {
                projectType.put("noStartProjects", 0);
            } else {
                projectType.put("noStartProjects", noStart);
            }
            projectTypeMapList.add(projectType);
        }

        int start = 0;
        int noStart = 0;
        for (NewProjects projects : resList) {
            if ("4".equals(projects.getStatus())) {
                start++;
            } else {
                noStart++;
            }
        }

        result.put("projectSum", resList.size());
        result.put("start", start);
        result.put("noStart", noStart);
        result.put("money", moneyCount);
        result.put("industryType", industryTypeMapList);
        result.put("projectType", projectTypeMapList);

        return result;
    }

    /**
     * 项目概览
     *
     * @param data
     * @return
     */
    public Map<String, String> generateOverview(Map<String, Object> data) {
        Map<String, String> resultMap = new TreeMap<>();
        //计划总投资
        double money = (double) data.get("money");  //万元
        Format fm1 = new DecimalFormat("#,###.00");
        int projectSum = (int) data.get("projectSum");      //总项目数
        int start = (int) data.get("start");
        //开工率
        double startRatio = start * 100.0 / projectSum;
        BigDecimal b = new BigDecimal(startRatio);
        startRatio = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        List<Map<String, Object>> projectTypeList = (List<Map<String, Object>>) data.get("projectType");
        String yearProjectData = "";
        String yearProjectData2 = "";
        //结转项目跟计划新开工
        for (Map<String, Object> objectMap : projectTypeList) {
            yearProjectData = yearProjectData + objectMap.get("name") + "："
                    + objectMap.get("count") + "项，" + "年计划投资" + fm1.format((double) objectMap.get("money") / 10000) + "亿元。";
            yearProjectData2 = yearProjectData2 + objectMap.get("name") + "开工"
                    + objectMap.get("startProjects") + "项。";

        }

        String content1 = getCurrenYear() + "年重点项目安排：总的项目" + projectSum +
                "项，计划总投资" + fm1.format(money / 10000) + "亿元。其中" + yearProjectData;
        String content2 = "截止" + getCurrentMonth().substring(4, 6) + "月，全年动工项目" + start + "项，开工率" + startRatio + "%。其中" + yearProjectData2;
        resultMap.put("content1", content1);
        resultMap.put("content2", content2);
        return resultMap;
    }
}
